package UnionFind;

import java.util.HashSet;
import java.util.Set;

public class _200_NumberofIslands {
    private Set<Integer> sets;
    private int[][] ids;
    private char[][] grid;
    private int rows;
    private int columns;
    public int numIslands_1(char[][] grid) {
        if(grid.length==0) return 0;
        this.rows = grid.length;
        this.columns = grid[0].length;
        this.ids = new int[rows][columns];
        this.grid = new char[rows][columns];
        int count = 0;
        this.sets = new HashSet<>();
        this.grid = grid;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                ids[i][j] = i * 10000 + j;
            }
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (grid[i][j] == '1') {
                    if (!sets.contains(ids[i][j])) {
                        count++;
                        sets.add(ids[i][j]);
                        depthfirst(i,j,ids[i][j]);
                    }
                }
            }
        }
        return count;
    }
        /*for (int i = 0; i < N; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == '1') {
                        count++;
                        if (i - 1 >= 0 && grid[i - 1][j] == '1') {
                            count--;
                        } else if (j - 1 >= 0 && grid[i][j - 1] == '1') {
                            count--;
                        } else if (i + 1 < N && grid[i + 1][j] == '1') {
                            count--;
                        } else if (j + 1 < grid[0].length && grid[i][j + 1] == '1') {
                            count--;
                        }
                    }
                }
        }*/


    public void depthfirst(int r, int c, int id) {
        if (r - 1 >= 0 && grid[r - 1][c] == '1' && ids[r - 1][c] != id) {
            ids[r - 1][c] = id;
            depthfirst(r - 1, c, id);
        }
        if (r + 1 < rows && grid[r + 1][c] == '1' && ids[r + 1][c] != id) {
            ids[r+1][c] = id;
            depthfirst(r + 1, c, id);
        }
        if (c - 1 >= 0 && grid[r][c - 1] == '1' && ids[r][c - 1] != id) {
            ids[r][c-1] = id;
            depthfirst(r, c - 1, id);
        }
        if (c + 1 < columns && grid[r][c + 1] == '1' && ids[r][c + 1] != id) {
            ids[r][c + 1] = id;
            depthfirst(r, c + 1, id);
        }
    }

    int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length==0) {
            return 0;
        }
        this.rows = grid.length;
        this.columns = grid[0].length;
        this.grid = grid;
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    depthfirst(i, j);
                }
            }
        }
        return count;
    }

    public void depthfirst(int r, int c) {
        if (r < 0 || r >= rows || c < 0 || c >= columns) {
            return;
        }
        if (grid[r][c] == '1') {
            grid[r][c] = '0';
            for (int[] dir : dirs) {
                depthfirst(r + dir[0], c + dir[1]);
            }
        }
    }
}
